/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.groovy

import java.nio.channels.FileChannel
import java.io.FileInputStream
import java.io.FileOutputStream

/**
 *
 * @author lukasz
 */
class Utils
{
    // copies the source file to the given target file
    static boolean copyFile(File source, File target)
    {
        try
        {
            target.withWriter() { file ->
                source.eachLine() { line ->
                    file.writeLine(line);
                }
            }
        }
        catch (Exception)
        {
            println("Error opening file!")
            return false;
        }

        return true;
    }

    static boolean copyFileBinary(File source, File target)
    {
        try
        {
            FileChannel sourceChannel = new FileInputStream(source).getChannel()
            FileChannel targetChannel= new FileOutputStream(target).getChannel()

            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size())

            sourceChannel.close()
            targetChannel.close()
        }
        catch (Exception e)
        {
            println("Error copying file!" + e.getMessage())
            return false
        }

        return true
    }

    // this class shouldn't be instantiated, so private constructor'
    private Utils()
    { }
}

